<?php
//消费券服务

namespace App\Services;

use App\Repositories\CouponReceiveRepository;
use App\Repositories\SubsidyBillRepository;
use Carbon\Carbon;

class SubsidyBillService
{

    /**
     * 获取列表
     * @param $query
     * @param $offset
     * @param $limit
     * @param $orderType
     * @return array|mixed
     */
    public static function getList($query = [], $offset = 0, $limit = 20, $orderType = 0)
    {
        //获取证书关联信息
        $repo = new SubsidyBillRepository();
        $repo->buildQuery($query);

        return $repo
            ->getList($offset, $limit, $orderType)
            ->toarray();
    }

    /**
     * 获取单条
     * @param $query
     * @return array
     */
    public static function getRow($query = [])
    {
        //获取证书关联信息
        $repo = new SubsidyBillRepository();
        $repo->buildQuery($query);
        $row = $repo
            ->getRow();
        if (!$row) {
            return [];
        }

        return $row->toArray();
    }

    /**
     * 获取总数
     * @param $query
     * @return array
     */
    public static function getTotal($query = [])
    {
        //获取证书关联信息
        $repo = new SubsidyBillRepository();
        $repo->buildQuery($query);

        return $repo->getTotal();
    }


    /**
     * 保存
     * @param $query
     * @return array
     */
    public static function save($data = [])
    {
        if (empty($data['activity_id']) || empty($data['receiver_id']) || empty($data['out_bill_no'])) {
            return resultError('参数缺失');
        }
        $repo = new SubsidyBillRepository();
        $res = $repo->save($data);
        if (!$res) {
            return resultError('保存失败');
        }

        return resultSuccess([
            'id' => $res,
        ], '保存成功');
    }

    /**
     * 更新核销状态
     * @param $id
     * @param $receiveStatus
     * @return array
     */
    public static function updateStatus($id, $status, $reason)
    {
        $data = [
            'status' => $status,
        ];
        if ($status == 5) {
            //转账失败
            $data['fail_reason'] = $reason;
        }
        $res = (new SubsidyBillRepository)->updateFields($id, $data);
        if (!$res) {
            return resultError('更新失败');
        }

        return resultSuccess([], '更新成功');
    }

    /**
     * 转换微信单据状态
     * @param $state
     * @return int
     */
    public static function convertWxStateToSubsidyBillStatus($state = '') {
        $status = 0; //单据处理状态：1. 单据处理中；2.待用户确认；3.转账处理中；4.转账成功；5.转账失败；
        switch ($state) {
            case 'ACCEPTED': //单据已受理
            case 'PROCESSING': //单据处理中，转账结果尚未明确，如一直处于此状态，建议检查账户余额是否足够
                $status = 1; //单据处理中
                break;
            case 'WAIT_USER_CONFIRM': //单据待用户确认，可拉起微信收款确认页面进行收款确认
                $status = 2; //待用户确认
                break;
            case 'TRANSFERING': //转账中，转账结果尚未明确，可拉起微信收款确认页面再次重试确认收款
                $status = 3; //转账处理中
                break;
            case 'SUCCESS': //转账成功
                $status = 4; //转账成功
                break;
            case 'FAIL': //转账失败
                $status = 5; //转账失败
                break;
        }

        return $status;
    }

}
